;thread for tapping screws type b
;outputs; [body]
;
;simplified thread?
{If:[sel_th]=Simplified}
	_Polyline _PersistentClose=_No 0,[dminth]/2,[tipstart] _Mode=_Line 0,[d]/2,[tipstart]-(([d]-[dminth])/2) 0,[d]/2,[tipend] 0,[dt]/2,-[l] 0,0,-[l] _Enter
	_SelLast
	{Revolve:0,0,0:0,0,[l]}
	{Set:[thread]={SelectedObjs}}
	{SetName:[npth]}
	_SelNone
{EndIf:}
;
;detailed thread?
{If:[sel_th]=Detailed}
	{Set:[cth]=0.004}
	{If:[d]>0.164}
		{Set:[cth]=0.006}
	{EndIf:}
	{Set:[cth]={ToDocUnits:[cth]}}
	;
	;main thread spiral
	{Set:[turns]={Math:(abs([tipend]-[tipstart])/[pth])-0.25}}
	_Spiral 0,0,[tipend]-[pth] 0,0,[tipend]+(([turns]-1)*[pth]) _Mode=_Turns _Turns=[turns] 0,[d]/2,[tipend]-[pth] 0,[d]/2,[tipend]+(([turns]-1)*[pth]) _Enter
	_SelLast
	{Set:[spiral]={SelectedObjs}}	
	_SelNone
	;
	;thread section
	{Set:[SectionID]={Guid}}
	_Polyline _PersistentClose=_No 0,([d]/2)-([pth]*0.6),[tipend]-[pth] _Mode=_Line 0,([d]/2)-([pth]*0.6),[tipend]-[pth]+([cth]/2)+(tan(30d)*([pth]*0.6)) 0,[d]/2,[tipend]-[pth]+([cth]/2) 0,[d]/2,[tipend]-[pth]-([cth]/2) 0,[d]/2,[tipend]-[pth]+([cth]/2) 0,([d]/2)-([pth]*0.6),[tipend]-[pth]-([cth]/2)-(tan(30d)*([pth]*0.6)) _Close
	_SelLast
	{Set:[section]={SelectedObjs}}
	{SetName:[SectionID]}
	{Duplicate:[section]}
	_Rotate _Copy=_No 0,0 {ObjectSnap:[spiral]:StartXY} {ObjectSnap:[spiral]:EndXY}
	_Move 0,0,{ObjectSnap:[spiral]:StartZ} 0,0,{ObjectSnap:[spiral]:EndZ}
	;
	;thread end near the head
	_Spiral 0,0,[tipend]+(([turns]-1)*[pth]) 0,0,[tipend]+([turns]*[pth]) _Mode=_Turns _Turns=1 0,[d]/2,[tipend]+(([turns]-1)*[pth]) 0,([dminth]/2)-[tol],[tipend]+([turns]*[pth]) _Enter
	_SelLast
	{Set:[spiral2]={SelectedObjs}}
	_Rotate _Copy=_No 0,0 {ObjectSnap:[spiral2]:StartXY} {ObjectSnap:[spiral]:EndXY}
	_SelNone
	;
	_SelId [section]
	{Duplicate:[section]}
	_Move 0,[d]/2,{ObjectSnap:[spiral]:StartZ} 0,([dminth]/2)-[tol],{ObjectSnap:[spiral2]:EndZ}
	_Rotate _Copy=_No 0,0 {ObjectSnap:[spiral]:StartXY} {ObjectSnap:[spiral2]:EndXY}
	_SelNone
	;
	_SelId [spiral]
	_SelId [spiral2]
	_Join
	{Set:[spiral]={SelectedObjs}}
	_SelNone
	;
	;sweep and join to the body
	-_Sweep1 '_SelID [spiral] '-_SelName [SectionID] _Enter _Natural _Enter _Style=_Roadlike 0,0,0 0,0,[tipend] _Simplify=_None _Closed=_No _ShapeBlending=_Local _Enter
	_SelLast
	{Cap:[tol]}
	{Set:[thread]={SelectedObjs}}
	_SelNone
	;
	;unfinished crest
	_Polyline _PersistentClose=_No 0,([dt]/2)-([tol]*2),-[l] _Mode=_Line 0,([d]/2)+([tol]*2),[tipend] 0,[dk]/2,[tipend] 0,[dk]/2,-[l] _Close
	_SelLast
	{Revolve:0,0,0:0,0,[l]}
	{Set:[obj]={SelectedObjs}}
	{BooleanDifference:[tol]:[thread]:[obj]}
	'_SelID [body]
	{BooleanUnion:[tol]}
	{Set:[body]={SelectedObjs}}
	_SelNone
	;
	;delete the sweep curves
	_SelID [spiral]
	_Delete
	-_SelName [SectionID]
	_Delete
{EndIf:}